package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.view.View;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/20.
 */

public class ShareModel extends ContextObservable {
    // 微博分享
    public static final int SHARE_SINA = 0;
    // 微信好友分享
    public static final int SHARE_WECHAT = 1;
    // 微信朋友圈分享
    public static final int SHARE_WECHAT_GROUP = 2;
    // QQ好友分享
    public static final int SHARE_QQ = 3;
    // QQ空间分享
    public static final int SHARE_QQ_SPACE = 4;

    public static final String SHARE_TITLE = "国内顶尖的董秘学习与交流平台";
    public static final String SHARE_TEXT = "专家在线视频课程，渗透挂牌前后实操，业内专家心得分享，汇聚圈内精彩资讯";
    public static final String SHARE_IMG = "http://dmyjr.oss-cn-beijing.aliyuncs.com/20161226/11979148273455526651.png";

    private String shareType;
    private int id;
    private ListBindingAdapter<ShareItem> shareItems;
    private Callback shareCallback;

    public void setShareCallback(Callback shareCallback) {
        this.shareCallback = shareCallback;
    }

    public String getShareType() {
        return shareType;
    }

    public void setShareType(String shareType) {
        this.shareType = shareType;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Bindable
    public ListBindingAdapter<ShareItem> getShareItems() {
        return shareItems;
    }

    public ShareModel(Context context) {
        super(context);
        shareItems = new ListBindingAdapter<ShareItem>(context, BR.shareItem);
        init();
    }

    private void init() {
        ShareItem item = new ShareItem(getContext(), R.layout.i_share);
        item.setTitle("微博");
        item.setType(SHARE_SINA);
        item.setIcon(R.mipmap.share01);
        item.setParent(this);
        shareItems.add(item);

        item = new ShareItem(getContext(), R.layout.i_share);
        item.setTitle("微信");
        item.setType(SHARE_WECHAT);
        item.setIcon(R.mipmap.share02);
        item.setParent(this);
        shareItems.add(item);

        item = new ShareItem(getContext(), R.layout.i_share);
        item.setTitle("朋友圈");
        item.setType(SHARE_WECHAT_GROUP);
        item.setIcon(R.mipmap.share03);
        item.setParent(this);
        shareItems.add(item);

        item = new ShareItem(getContext(), R.layout.i_share);
        item.setTitle("QQ");
        item.setType(SHARE_QQ);
        item.setIcon(R.mipmap.share04);
        item.setParent(this);
        shareItems.add(item);

        item = new ShareItem(getContext(), R.layout.i_share);
        item.setTitle("QQ空间");
        item.setType(SHARE_QQ_SPACE);
        item.setIcon(R.mipmap.share05);
        item.setParent(this);
        shareItems.add(item);
    }

    public void onShare(ShareItem item) {
//        if (shareCallback != null) {
//            if (shareCallback.onShare(this.getShareType(), this.getId(), item)) {
        SHARE_MEDIA share_media = SHARE_MEDIA.QQ;
        if (shareCallback != null) {
            shareCallback.onShare(item);
        }
        switch (item.getType()) {
            case SHARE_QQ:
                share_media = SHARE_MEDIA.QQ;
                break;
            case SHARE_QQ_SPACE:
                share_media = SHARE_MEDIA.QZONE;
                break;
            case SHARE_SINA:
                share_media = SHARE_MEDIA.SINA;
//                UMImage image = new UMImage(getContext(), SHARE_IMG);
//                new ShareAction((Activity) getContext())
//                        .setPlatform(share_media)
//                        .setCallback(umShareListener)
//                        .withMedia(image)
//                        .withTitle(SHARE_TITLE)
//                        .share();
//                new ShareAction(ShareActivity.this).withText("hello")
//                        .withMedia(image)
//                        .withTargetUrl(xxx)
//                        .withTitle(xxxx)
//                        .share();
                break;
            case SHARE_WECHAT:
                share_media = SHARE_MEDIA.WEIXIN;
                break;
            case SHARE_WECHAT_GROUP:
                share_media = SHARE_MEDIA.WEIXIN_CIRCLE;
                break;
        }
        new ShareAction((Activity) getContext())
                .setPlatform(share_media)
                .setCallback(umShareListener)
                .setShareContent(item.getShareData())
                .share();
    }

    public void onCancel(View view) {
        Activity activity = (Activity) getContext();
        activity.finish();
    }

    public interface Callback {

        void onShare(ShareItem shareItem);

        void onShareSuccess();
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Activity activity = (Activity) getContext();
            activity.finish();
            if (shareCallback != null) {
                shareCallback.onShareSuccess();
            }
//            Toast.makeText((Activity) getContext(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText((Activity) getContext(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            Toast.makeText((Activity) getContext(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
}
